/**场景1
 * 
 * 假定有一个对象数组A，获取数组中指定类型的对象放到B数组中
 */
var products=[
    {name:"cucumber",type:"vegetable"},
    {name:"banana",type:"fruit"},
    {name:"celery",type:"vegetable"},
    {name:"orange",type:"fruit"}
]
var fruits=[];
//es5
for(let i=0;i<products.length;i++){
    if(products[i].type==="fruit"){
        fruits.push(products[i]);
    }
}
console.log(fruits);

//es6
var fruits2 =products.filter(function(product){
    return product.type==="fruit";
});
console.log(fruits2);


 /**场景2
  * 
  * 假定有一个对象数组A，过滤掉不满足一下条件的对象
  * 条件：蔬菜 数量大于0，价格小于10
  */

var products2=[
    {name:"cucumber",type:"vegetable",quantity:3,price:11},
    {name:"banana",type:"fruit",quantity:2,price:10},
    {name:"celery",type:"vegetable",quantity:5,price:3},
    {name:"orange",type:"fruit",quantity:3,price:6}
];
var vegetable2=products2.filter(function(product2){
    return product2.type==="vegetable"&&product2.quantity>0&&product2.price<10;
})
console.log(vegetable2);



/**场景3
 * 
 * 假定有两个数组A,B，根据A中id值，过滤掉B数组不符合的数据
 */

var post={id:4,title:"Javascript"};
var comments=[
    {postId:4,content:"Angular4"},
    {postId:2,content:"Vue.js"},
    {postId:3,content:"Node.js"},
    {postId:4,content:"React.js"}
];

function commentsForPost(post,comment){
    return comment.filter(function(comment){
        return comment.postId === post.id
    }
)};
console.log(commentsForPost(post,comments));